// pages/welcome/welcome.js

const APP_ID = 'wx41acab51bd677ea7'; //输入小程序appid
const APP_SECRET = 'dab095b644777cf1b4f775d933e37259'; //输入小程序app_secret
var OPEN_ID = '' //储存获取到openid
var SESSION_KEY = '' //储存获取到session_key
const app = getApp();
var r, act_id;
const infoUtils = require("../../utils/userInfo.js");
const api = require("../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    openid: null,
    session_key: null,

  },
  onLoad: function(options) {
    r = this;
    act_id = options.act_id;
    console.log('act_id = ' + act_id)
  },
  onGotUserInfo: function(e) {
    var _this = this;
    if (e.detail.userInfo) {
      var wxUserInfo = e.detail.userInfo;
      wx.login({
        success: function(redd) {
          if (redd.code) {
            wx.request({
              url: api.login.getOpenId + '/' + redd.code,
              success: function(back) {
                /** 用户的open_id */
                var openId = back.data.data;
                if (back.statusCode == 200 && back.data.code == 200) {
                  /** 获取用户信息，存入或者更新到数据库 */
                  wx.getUserInfo({
                    lang: 'zh_CN',
                    success: function(result) {
                      let wx_user = result.userInfo;
                      wx.request({
                        url: api.login.login,
                        data: {
                          openId: openId,
                          nickName: wx_user.nickName,
                          avatarUrl: wx_user.avatarUrl,
                          gender: wx_user.gender,
                          province: wx_user.province,
                          city: wx_user.city,
                        },
                        header: {
                          'content-type': 'application/x-www-form-urlencoded' // 默认值
                        },
                        method: 'post',
                        success: function(backResult) {  console.log('获取用户信息成功') }
                      })
                    },
                    fail: function(res) { console.log('获取用户信息失败') }
                  })


                  /** 赋值 */ 
                  wxUserInfo.openId = openId;
                  app.globalData.wxUserInfo = wxUserInfo;
                  /** 全局的 open_id 赋值 */
                  app.globalData.open_id = openId;

                  var value = wx.getStorageSync('inviteOpenId');
                  if (app.globalData.shareType) {
                    if (app.globalData.shareType == 0) {
                      wx.reLaunch({
                        url: `/pages/index/index?id=${app.globalData.userId}&inviteOpenId=${value}&shareType=0&inviteType=1`
                      });
                      setTimeout(() => {
                        wx.setStorageSync('inviteOpenId', '');
                      }, 500)
                    } else if (app.globalData.shareType == 1) {
                      wx.reLaunch({
                        url: `/pages/index/index?id=${app.globalData.userId}&shareType=1`
                      });
                    } else if (app.globalData.shareType == 2) {
                      wx.reLaunch({
                        url: `/pages/index/index?id=${app.globalData.userId}&shareType=2&inviteOpenId=${value}`
                      });
                    } else if (app.globalData.shareType == 3) {
                      wx.reLaunch({
                        url: `/pages/discover/discover?id=${app.globalData.userId}&shareType=3`
                      });

                    } else if (app.globalData.shareType == 4) {
                      wx.reLaunch({
                        url: `/pages/audio/audio?id=${app.globalData.userId}&shareType=4`
                      });

                    } else if (app.globalData.shareType == 7) {
                      wx.reLaunch({
                        url: `/pages/audio/audio?id=${app.globalData.userId}&inviteOpenId=${value}&shareType=7&inviteType=2`
                      });
                      setTimeout(() => {
                        wx.setStorageSync('inviteOpenId', '');
                      }, 500)
                    }
                    setTimeout(() => {
                      app.globalData.shareType = '';
                      app.globalData.userId = '';

                    }, 1000)
                  } else {
                    /** 返回上一级页面 */
                    wx.navigateBack({ delta: 1 })
                  }
                } else {
                  wx.showModal({
                    title: '授权失败',
                    content: '请重新点击',
                    showCancel: false
                  })
                }
              }
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /** 不授权返回主页面 */
  not_empower: function() {
    /** 返回之前的页面 */
    wx.navigateBack({ delta: 1 })
    // wx.reLaunch({
    //   url: '/pages/index/index'
    // })
  }

})